﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.Editors.Code;
using System.IO;
using EnvDTE;
using EnvDTE80;
using System.Reflection;
using com.code.Editors.Code;


namespace com.Editors.UserControls
{
    public partial class UIAddDatasource : Form
    {
        com.code.Editors.Code.GuidDataColumn colObj;
        IServiceProvider provider = null;
        IDictionaryService dictionaryService = null;
        ArrayList listTables = null;
        ArrayList listSPs = null;

#region "Properties"
             
#endregion

        public UIAddDatasource(com.code.Editors.Code.GuidDataColumn col, IServiceProvider provider, Point p)
        {
            InitializeComponent();
            this.provider = provider;
            this.Left = p.X;
            this.Top = p.Y;
            this.ShowInTaskbar = false;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.colObj = col;
            //this.Capture = true;
            //SetAssemblyClasses(cmbAddtoClass);
            //SetAssemblyClasses(cmbDALClasses);
            BindCombo();
        }

        private void SetAssemblyClasses(ComboBox cmb)
        {
            //System.Reflection.
            dictionaryService = provider.GetService(typeof(IDictionaryService)) as IDictionaryService;

            //dictionaryService.SetValue("ClassName", "Test");
            EnvDTE.Project prj = (EnvDTE.Project)
             dictionaryService.GetValue("DALProject");
            
            if (prj != null)
            {
                Assembly assem = Assembly.LoadFile(GetAssemblyPath(prj));
                if (assem != null)
                {
                    Type[] types = assem.GetTypes();
                    foreach (Type t in types)
                    {
                        if (t.IsClass)
                            cmb.Items.Add(t.Name);
                    }
                }
            }
        }

        private string GetAssemblyPath(EnvDTE.Project project)
        {
            Property outputPath =
              project.ConfigurationManager.ActiveConfiguration.
              Properties.Item("OutputPath");
            Property assemblyName = project.Properties.Item("OutputFileName");
            return GetFolderPath(project.FullName) + "\\" +
              (string)outputPath.Value + (string)assemblyName.Value;
        }

        private string GetFilePath(object o)
        {
            if (o is EnvDTE.Solution)
            {
                EnvDTE.Solution solution = (EnvDTE.Solution)o;
                return solution.FullName;
            }
            else if (o is EnvDTE.Project)
            {
                EnvDTE.Project project = (EnvDTE.Project)o;
                
                    return project.FullName;                
            }
            else if (o is EnvDTE.ProjectItem)
            {
                EnvDTE.ProjectItem projectItem = (EnvDTE.ProjectItem)o;

                // A 'ProjectItem' can be a sub project, a folder (or a file)
                if (projectItem.SubProject != null)
                {
                    return GetFilePath(projectItem.SubProject);
                }
                else
                {
                    // we return the full path of the folder (or the file)
                    //_logger.DebugFormat("The selected item is a '{0}' " +
                    //   "folder or a file : ", projectItem.get_FileNames(0));
                    return projectItem.get_FileNames(0);
                }
            }
            else
            {
                throw new System.NullReferenceException("Full path" +
                          " not found for the selected item");
            }
        }

        private string GetFolderPath(string filePath)
        {
            FileInfo info = new FileInfo(filePath);
            return info.Directory.FullName;
        }
        private void chkNewDS_CheckedChanged(object sender, EventArgs e)
        {
            gbNewDS.Enabled = chkNewDS.Checked;
            gbOldDS.Enabled = ! chkNewDS.Checked;
            
        }

        private void btnBind_Click(object sender, EventArgs e)
        {
            colObj.IsNewDatasource = chkNewDS.Checked;
            colObj.PrevClassName = cmbDALClasses.Text;
            colObj.PrevMethod = this.cmbClassMethods.Text;
            colObj.IsGenNewClass = this.rBtnGentNewClass.Checked;
            colObj.NewClassName = txtClasstoAdd.Text;

            colObj.IsSPProvided = rBtnNewSP.Checked;
            colObj.NewSPName = txtNewMethod.Text;
            colObj.IsTableProvided = rBtnTable.Checked;
            colObj.NewTableName = cmbNewTableName.Text;
            
            colObj.IsGenNewClass = rBtnGentNewClass.Checked;
            colObj.NewClassName = txtClasstoAdd.Text;
            colObj.ExistClassAddNewMethod = cmbAddtoClass.Text;
            colObj.NewMethodName = this.txtNewMethod.Text;
            colObj.TextDisplay = this.cmbDispCol.Text;
            colObj.ValueDisplay = this.cmbValCol.Text;
            this.Hide();
        }

        private void rBtnNewSP_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rBtnTable_CheckedChanged(object sender, EventArgs e)
        {
            
            
        }

        private void rBtnExistClass_CheckedChanged(object sender, EventArgs e)
        {
            cmbAddtoClass.Enabled = rBtnExistClass.Checked;
            txtClasstoAdd.Enabled = ! rBtnExistClass.Checked;
        }

        private void rBtnGentNewClass_CheckedChanged(object sender, EventArgs e)
        {
            cmbAddtoClass.Enabled = ! rBtnGentNewClass.Checked;
            txtClasstoAdd.Enabled = rBtnGentNewClass.Checked;
        }

        private void btnCols_Click(object sender, EventArgs e)
        {
            if (rBtnTable.Checked)
            {
                com.code.Editors.Code.BindTableData obj = new BindTableData(this);
                obj.setTableColumnsSchema(cmbNewTableName.Text);
                obj.Show();
            }
            else if (rBtnNewSP.Checked)
            {
                com.code.Editors.Code.BindTableData obj = new BindTableData(this);
                obj.setSPColumns(cmbSPName.Text);
                obj.Show();
            }
        }

        private ArrayList GetUserTables()
        {
            if(listTables == null)
            {
                SchemaDAL objSc = new SchemaDAL(DataForm.GetConStr());
                listTables = objSc.GetTables();
            }
            return listTables;
            
        }

        private ArrayList GetUserSPs()
        {
            if (listSPs == null)
            {
                SchemaDAL objSc = new SchemaDAL(DataForm.GetConStr());
                listSPs = objSc.GetUserSPs();
            }
            return listSPs;

        }

        private void BindCombo()
        {
            cmbSPName.Items.Clear();
            cmbNewTableName.Items.Clear();
            foreach (GuidDataTable tbl in GetUserTables())
            {
                cmbNewTableName.Items.Add(tbl.TableName);
            }

            foreach (string sp in GetUserSPs())
            {
                cmbSPName.Items.Add(sp);
            }
        }

        public void BindCols(ArrayList list)
        { 
            foreach(string col in list)
            {
                colObj.BindString += col +",";
                cmbDispCol.Items.Add(col);
                cmbValCol.Items.Add(col);
            }
            colObj.BindString.Remove(colObj.BindString.LastIndexOf(","),1);
            colObj.BindString += " from "+ cmbNewTableName.Text;
        }
    }
}
